/**
 * @author songchao 42894870@qq.com
 * 2017年4月17日
 */
package com.tanovo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.tanovo.exception.ServiceException;
import com.tanovo.service.ISmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * 没有正式测试
 *
 * @author songchao 42894870@qq.com
 */
@Service
@Slf4j
public class SmsServiceImpl implements ISmsService {

    @Value("${aliyun.sms.accesskeyid}")
    private String aliyun_sms_accesskeyid;
    @Value("${aliyun.sm.accesssecret}")
    private String aliyun_sms_accesssecret;
    @Value("${aliyun.sms.tpid}")
    private String aliyun_sms_tpid;


    @Override
    public void send(String phone, Long code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyun_sms_accesskeyid, aliyun_sms_accesssecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "天帷信安");
        request.putQueryParameter("TemplateCode", aliyun_sms_tpid);
        request.putQueryParameter("TemplateParam", "{code:\"" + code + "\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
            String resp = response.getData();
            log.info("电话：{}  阿里云发送短信返回信息：{}", phone, resp);
            JSONObject json = JSONObject.parseObject(resp);
            String retCode = json.getString("Code");
            if (retCode.equalsIgnoreCase("isv.AMOUNT_NOT_ENOUGH")) {
                log.error("发送阿里云短信出错:阿里云短信欠费");
                throw new ServiceException("阿里云短信欠费");
            }
        } catch (ServerException e) {
            e.printStackTrace();
            log.error("发送阿里云短信出错:", e);
            throw new ServiceException("阿里云短信出错");
        } catch (ClientException e) {
            log.error("发送阿里云短信出错:", e);
            e.printStackTrace();
            throw new ServiceException("阿里云短信出错");
        }

    }


    public static void main(String[] args) {
        String msg="您的注册码为%s,谢谢注册!";
        System.out.println("Math.random得到小数");
        System.out.println(Math.round((Math.random()+1) * 1000));
        System.out.println("Random");
        System.out.println(new Random().nextInt(9999));
        System.out.println("字符串前面补0的话就这样String.format");
        System.out.println(String.format("%04d",new Random().nextInt(9999)));
    }
}
